/**
 * Copyright © 2015-2020 <a href="http://www.jeeplus.org/">JeePlus</a> All rights reserved.
 */
package com.jeeplus.modules.ticket.crowdleadershipteach.web;

import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.validation.ConstraintViolationException;

import com.jeeplus.modules.ticket.crowdleadershipfu.entity.CrowdLeadershipFu;
import org.apache.shiro.authz.annotation.Logical;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import com.google.common.collect.Lists;
import com.jeeplus.common.utils.DateUtils;
import com.jeeplus.common.json.AjaxJson;
import com.jeeplus.core.persistence.Page;
import com.jeeplus.core.web.BaseController;
import com.jeeplus.common.utils.StringUtils;
import com.jeeplus.common.utils.excel.ExportExcel;
import com.jeeplus.common.utils.excel.ImportExcel;
import com.jeeplus.modules.ticket.crowdleadershipteach.entity.CrowdLeadershipTeach;
import com.jeeplus.modules.ticket.crowdleadershipteach.service.CrowdLeadershipTeachService;
import redis.clients.jedis.Jedis;

/**
 * 领导班子(教学群众)Controller
 * @author 刘明星
 * @version 2021-09-15
 */
@RestController
@RequestMapping(value = "/ticket/crowdleadershipteach/crowdLeadershipTeach")
public class CrowdLeadershipTeachController extends BaseController {

	@Autowired
	private CrowdLeadershipTeachService crowdLeadershipTeachService;

	@ModelAttribute
	public CrowdLeadershipTeach get(@RequestParam(required=false) String id) {
		CrowdLeadershipTeach entity = null;
		if (StringUtils.isNotBlank(id)){
			entity = crowdLeadershipTeachService.get(id);
		}
		if (entity == null){
			entity = new CrowdLeadershipTeach();
		}
		return entity;
	}

	/**
	 * 领导班子(教学群众)列表数据
	 */
//	@RequiresPermissions("ticket:crowdleadershipteach:crowdLeadershipTeach:list")
	@GetMapping("list")
	public AjaxJson list(CrowdLeadershipTeach crowdLeadershipTeach, HttpServletRequest request, HttpServletResponse response) {
		Jedis jedis = new Jedis("localhost");
		String admin = request.getParameter("admin");
		if(!admin.equals("admin")){
			String key = request.getParameter("key");
			if(jedis.exists(key)){
				return AjaxJson.success("redis").put("value",jedis.get(key));
			}
		}
		Page<CrowdLeadershipTeach> page = crowdLeadershipTeachService.findPage(new Page<CrowdLeadershipTeach>(request, response), crowdLeadershipTeach);
		return AjaxJson.success().put("page",page);
	}

	/**
	 * 根据Id获取领导班子(教学群众)数据
	 */
//	@RequiresPermissions(value={"ticket:crowdleadershipteach:crowdLeadershipTeach:view","ticket:crowdleadershipteach:crowdLeadershipTeach:add","ticket:crowdleadershipteach:crowdLeadershipTeach:edit"},logical=Logical.OR)
	@GetMapping("queryById")
	public AjaxJson queryById(CrowdLeadershipTeach crowdLeadershipTeach) {
		return AjaxJson.success().put("crowdLeadershipTeach", crowdLeadershipTeach);
	}
	@PostMapping("updateGood")
	public AjaxJson updateGood(CrowdLeadershipTeach crowdLeadershipTeach, Model model) throws Exception{
		/**
		 * 后台hibernate-validation插件校验
		 */
		String errMsg = beanValidator(crowdLeadershipTeach);
		if (StringUtils.isNotBlank(errMsg)){
			return AjaxJson.error(errMsg);
		}
		//新增或编辑表单保存
		crowdLeadershipTeachService.updateGood(crowdLeadershipTeach);//保存
		return AjaxJson.success("保存数据成功");
	}
	@PostMapping("updateCompetent")
	public AjaxJson updateCompetent(CrowdLeadershipTeach crowdLeadershipTeach, Model model) throws Exception{
		/**
		 * 后台hibernate-validation插件校验
		 */
		String errMsg = beanValidator(crowdLeadershipTeach);
		if (StringUtils.isNotBlank(errMsg)){
			return AjaxJson.error(errMsg);
		}
		//新增或编辑表单保存
		crowdLeadershipTeachService.updateCompetent(crowdLeadershipTeach);//保存
		return AjaxJson.success("保存数据成功");
	}
	@PostMapping("updateBasisCompetentList")
	public AjaxJson updateBasisCompetentList(CrowdLeadershipTeach crowdLeadershipTeach, Model model) throws Exception{
		/**
		 * 后台hibernate-validation插件校验
		 */
		String errMsg = beanValidator(crowdLeadershipTeach);
		if (StringUtils.isNotBlank(errMsg)){
			return AjaxJson.error(errMsg);
		}
		//新增或编辑表单保存
		crowdLeadershipTeachService.updateBasisCompetentList(crowdLeadershipTeach);//保存
		return AjaxJson.success("保存数据成功");
	}
	@PostMapping("updateNoCompetentList")
	public AjaxJson updateNoCompetentList(CrowdLeadershipTeach crowdLeadershipTeach, Model model) throws Exception{
		/**
		 * 后台hibernate-validation插件校验
		 */
		String errMsg = beanValidator(crowdLeadershipTeach);
		if (StringUtils.isNotBlank(errMsg)){
			return AjaxJson.error(errMsg);
		}
		//新增或编辑表单保存
		crowdLeadershipTeachService.updateNoCompetentList(crowdLeadershipTeach);//保存
		return AjaxJson.success("保存数据成功");
	}
	@PostMapping("updateZhengGood")
	public AjaxJson updateZhengGood(CrowdLeadershipTeach crowdLeadershipTeach, Model model) throws Exception{
		/**
		 * 后台hibernate-validation插件校验
		 */
		String errMsg = beanValidator(crowdLeadershipTeach);
		if (StringUtils.isNotBlank(errMsg)){
			return AjaxJson.error(errMsg);
		}
		//新增或编辑表单保存
		crowdLeadershipTeachService.updateZhengGood(crowdLeadershipTeach);//保存
		return AjaxJson.success("保存数据成功");
	}
	@PostMapping("updateZhengCompetent")
	public AjaxJson updateZhengCompetent(CrowdLeadershipTeach crowdLeadershipTeach, Model model) throws Exception{
		/**
		 * 后台hibernate-validation插件校验
		 */
		String errMsg = beanValidator(crowdLeadershipTeach);
		if (StringUtils.isNotBlank(errMsg)){
			return AjaxJson.error(errMsg);
		}
		//新增或编辑表单保存
		crowdLeadershipTeachService.updateZhengCompetent(crowdLeadershipTeach);//保存
		return AjaxJson.success("保存数据成功");
	}
	@PostMapping("updateZhengBasisCompetentList")
	public AjaxJson updateZhengBasisCompetentList(CrowdLeadershipTeach crowdLeadershipTeach, Model model) throws Exception{
		/**
		 * 后台hibernate-validation插件校验
		 */
		String errMsg = beanValidator(crowdLeadershipTeach);
		if (StringUtils.isNotBlank(errMsg)){
			return AjaxJson.error(errMsg);
		}
		//新增或编辑表单保存
		crowdLeadershipTeachService.updateZhengBasisCompetentList(crowdLeadershipTeach);//保存
		return AjaxJson.success("保存数据成功");
	}
	@PostMapping("updateZhengNoCompetentList")
	public AjaxJson updateZhengNoCompetentList(CrowdLeadershipTeach crowdLeadershipTeach, Model model) throws Exception{
		/**
		 * 后台hibernate-validation插件校验
		 */
		String errMsg = beanValidator(crowdLeadershipTeach);
		if (StringUtils.isNotBlank(errMsg)){
			return AjaxJson.error(errMsg);
		}
		//新增或编辑表单保存
		crowdLeadershipTeachService.updateZhengNoCompetentList(crowdLeadershipTeach);//保存
		return AjaxJson.success("保存数据成功");
	}
	@PostMapping("updateFanNoExistList")
	public AjaxJson updateFanNoExistList(CrowdLeadershipTeach crowdLeadershipTeach, Model model) throws Exception{
		/**
		 * 后台hibernate-validation插件校验
		 */
		String errMsg = beanValidator(crowdLeadershipTeach);
		if (StringUtils.isNotBlank(errMsg)){
			return AjaxJson.error(errMsg);
		}
		//新增或编辑表单保存
		crowdLeadershipTeachService.updateFanNoExistList(crowdLeadershipTeach);//保存
		return AjaxJson.success("保存数据成功");
	}
	@PostMapping("updateFanPointExistList")
	public AjaxJson updateFanPointExistList(CrowdLeadershipTeach crowdLeadershipTeach, Model model) throws Exception{
		/**
		 * 后台hibernate-validation插件校验
		 */
		String errMsg = beanValidator(crowdLeadershipTeach);
		if (StringUtils.isNotBlank(errMsg)){
			return AjaxJson.error(errMsg);
		}
		//新增或编辑表单保存
		crowdLeadershipTeachService.updateFanPointExistList(crowdLeadershipTeach);//保存
		return AjaxJson.success("保存数据成功");
	}
	@PostMapping("updateFanProminentList")
	public AjaxJson updateFanProminentList(CrowdLeadershipTeach crowdLeadershipTeach, Model model) throws Exception{
		/**
		 * 后台hibernate-validation插件校验
		 */
		String errMsg = beanValidator(crowdLeadershipTeach);
		if (StringUtils.isNotBlank(errMsg)){
			return AjaxJson.error(errMsg);
		}
		//新增或编辑表单保存
		crowdLeadershipTeachService.updateFanProminentList(crowdLeadershipTeach);//保存
		return AjaxJson.success("保存数据成功");
	}
	@PostMapping("updateSpeakGood")
	public AjaxJson updateSpeakGood(CrowdLeadershipTeach crowdLeadershipTeach, Model model) throws Exception{
		/**
		 * 后台hibernate-validation插件校验
		 */
		String errMsg = beanValidator(crowdLeadershipTeach);
		if (StringUtils.isNotBlank(errMsg)){
			return AjaxJson.error(errMsg);
		}
		//新增或编辑表单保存
		crowdLeadershipTeachService.updateSpeakGood(crowdLeadershipTeach);//保存
		return AjaxJson.success("保存数据成功");
	}
	@PostMapping("updateSpeakCompetent")
	public AjaxJson updateSpeakCompetent(CrowdLeadershipTeach crowdLeadershipTeach, Model model) throws Exception{
		/**
		 * 后台hibernate-validation插件校验
		 */
		String errMsg = beanValidator(crowdLeadershipTeach);
		if (StringUtils.isNotBlank(errMsg)){
			return AjaxJson.error(errMsg);
		}
		//新增或编辑表单保存
		crowdLeadershipTeachService.updateSpeakCompetent(crowdLeadershipTeach);//保存
		return AjaxJson.success("保存数据成功");
	}
	@PostMapping("updateSpeakBasisCompetentList")
	public AjaxJson updateSpeakBasisCompetentList(CrowdLeadershipTeach crowdLeadershipTeach, Model model) throws Exception{
		/**
		 * 后台hibernate-validation插件校验
		 */
		String errMsg = beanValidator(crowdLeadershipTeach);
		if (StringUtils.isNotBlank(errMsg)){
			return AjaxJson.error(errMsg);
		}
		//新增或编辑表单保存
		crowdLeadershipTeachService.updateSpeakBasisCompetentList(crowdLeadershipTeach);//保存
		return AjaxJson.success("保存数据成功");
	}
	@PostMapping("updateSpeakNoCompetentList")
	public AjaxJson updateSpeakNoCompetentList(CrowdLeadershipTeach crowdLeadershipTeach, Model model) throws Exception{
		/**
		 * 后台hibernate-validation插件校验
		 */
		String errMsg = beanValidator(crowdLeadershipTeach);
		if (StringUtils.isNotBlank(errMsg)){
			return AjaxJson.error(errMsg);
		}
		//新增或编辑表单保存
		crowdLeadershipTeachService.updateSpeakNoCompetentList(crowdLeadershipTeach);//保存
		return AjaxJson.success("保存数据成功");
	}
	@PostMapping("updateSpeakZhengGood")
	public AjaxJson updateSpeakZhengGood(CrowdLeadershipTeach crowdLeadershipTeach, Model model) throws Exception{
		/**
		 * 后台hibernate-validation插件校验
		 */
		String errMsg = beanValidator(crowdLeadershipTeach);
		if (StringUtils.isNotBlank(errMsg)){
			return AjaxJson.error(errMsg);
		}
		//新增或编辑表单保存
		crowdLeadershipTeachService.updateSpeakZhengGood(crowdLeadershipTeach);//保存
		return AjaxJson.success("保存数据成功");
	}
	@PostMapping("updateSpeakZhengCompetent")
	public AjaxJson updateSpeakZhengCompetent(CrowdLeadershipTeach crowdLeadershipTeach, Model model) throws Exception{
		/**
		 * 后台hibernate-validation插件校验
		 */
		String errMsg = beanValidator(crowdLeadershipTeach);
		if (StringUtils.isNotBlank(errMsg)){
			return AjaxJson.error(errMsg);
		}
		//新增或编辑表单保存
		crowdLeadershipTeachService.updateSpeakZhengCompetent(crowdLeadershipTeach);//保存
		return AjaxJson.success("保存数据成功");
	}
	@PostMapping("updateSpeakZhengBasisCompetentList")
	public AjaxJson updateSpeakZhengBasisCompetentList(CrowdLeadershipTeach crowdLeadershipTeach, Model model) throws Exception{
		/**
		 * 后台hibernate-validation插件校验
		 */
		String errMsg = beanValidator(crowdLeadershipTeach);
		if (StringUtils.isNotBlank(errMsg)){
			return AjaxJson.error(errMsg);
		}
		//新增或编辑表单保存
		crowdLeadershipTeachService.updateSpeakZhengBasisCompetentList(crowdLeadershipTeach);//保存
		return AjaxJson.success("保存数据成功");
	}
	@PostMapping("updateSpeakZhengNoCompetentList")
	public AjaxJson updateSpeakZhengNoCompetentList(CrowdLeadershipTeach crowdLeadershipTeach, Model model) throws Exception{
		/**
		 * 后台hibernate-validation插件校验
		 */
		String errMsg = beanValidator(crowdLeadershipTeach);
		if (StringUtils.isNotBlank(errMsg)){
			return AjaxJson.error(errMsg);
		}
		//新增或编辑表单保存
		crowdLeadershipTeachService.updateSpeakZhengNoCompetentList(crowdLeadershipTeach);//保存
		return AjaxJson.success("保存数据成功");
	}
	@PostMapping("updateSpeakFanNoExistList")
	public AjaxJson updateSpeakFanNoExistList(CrowdLeadershipTeach crowdLeadershipTeach, Model model) throws Exception{
		/**
		 * 后台hibernate-validation插件校验
		 */
		String errMsg = beanValidator(crowdLeadershipTeach);
		if (StringUtils.isNotBlank(errMsg)){
			return AjaxJson.error(errMsg);
		}
		//新增或编辑表单保存
		crowdLeadershipTeachService.updateSpeakFanNoExistList(crowdLeadershipTeach);//保存
		return AjaxJson.success("保存数据成功");
	}
	@PostMapping("updateSpeakFanPointExistList")
	public AjaxJson updateSpeakFanPointExistList(CrowdLeadershipTeach crowdLeadershipTeach, Model model) throws Exception{
		/**
		 * 后台hibernate-validation插件校验
		 */
		String errMsg = beanValidator(crowdLeadershipTeach);
		if (StringUtils.isNotBlank(errMsg)){
			return AjaxJson.error(errMsg);
		}
		//新增或编辑表单保存
		crowdLeadershipTeachService.updateSpeakFanPointExistList(crowdLeadershipTeach);//保存
		return AjaxJson.success("保存数据成功");
	}
	@PostMapping("updateSpeakFanProminentList")
	public AjaxJson updateSpeakFanProminentList(CrowdLeadershipTeach crowdLeadershipTeach, Model model) throws Exception{
		/**
		 * 后台hibernate-validation插件校验
		 */
		String errMsg = beanValidator(crowdLeadershipTeach);
		if (StringUtils.isNotBlank(errMsg)){
			return AjaxJson.error(errMsg);
		}
		//新增或编辑表单保存
		crowdLeadershipTeachService.updateSpeakFanProminentList(crowdLeadershipTeach);//保存
		return AjaxJson.success("保存数据成功");
	}
	/**
	 * 保存领导班子(教学群众)
	 */
//	@RequiresPermissions(value={"ticket:crowdleadershipteach:crowdLeadershipTeach:add","ticket:crowdleadershipteach:crowdLeadershipTeach:edit"},logical=Logical.OR)
	@PostMapping("save")
	public AjaxJson save(CrowdLeadershipTeach crowdLeadershipTeach, Model model) throws Exception{
		/**
		 * 后台hibernate-validation插件校验
		 */
		String errMsg = beanValidator(crowdLeadershipTeach);
		if (StringUtils.isNotBlank(errMsg)){
			return AjaxJson.error(errMsg);
		}
		//新增或编辑表单保存
		crowdLeadershipTeachService.save(crowdLeadershipTeach);//保存
		return AjaxJson.success("保存领导班子(教学群众)成功");
	}


	/**
	 * 批量删除领导班子(教学群众)
	 */
//	@RequiresPermissions("ticket:crowdleadershipteach:crowdLeadershipTeach:del")
	@DeleteMapping("delete")
	public AjaxJson delete(String ids) {
		String idArray[] =ids.split(",");
		for(String id : idArray){
			crowdLeadershipTeachService.delete(new CrowdLeadershipTeach(id));
		}
		return AjaxJson.success("删除领导班子(教学群众)成功");
	}

	/**
	 * 导出excel文件
	 */
	@RequiresPermissions("ticket:crowdleadershipteach:crowdLeadershipTeach:export")
    @GetMapping("export")
    public AjaxJson exportFile(CrowdLeadershipTeach crowdLeadershipTeach, HttpServletRequest request, HttpServletResponse response) {
		try {
            String fileName = "领导班子(教学群众)"+DateUtils.getDate("yyyyMMddHHmmss")+".xlsx";
            Page<CrowdLeadershipTeach> page = crowdLeadershipTeachService.findPage(new Page<CrowdLeadershipTeach>(request, response, -1), crowdLeadershipTeach);
    		new ExportExcel("领导班子(教学群众)", CrowdLeadershipTeach.class).setDataList(page.getList()).write(response, fileName).dispose();
    		return null;
		} catch (Exception e) {
			return AjaxJson.error("导出领导班子(教学群众)记录失败！失败信息："+e.getMessage());
		}
    }

	/**
	 * 导入Excel数据

	 */
	@RequiresPermissions("ticket:crowdleadershipteach:crowdLeadershipTeach:import")
    @PostMapping("import")
   	public AjaxJson importFile(@RequestParam("file")MultipartFile file, HttpServletResponse response, HttpServletRequest request) {
		try {
			int successNum = 0;
			int failureNum = 0;
			StringBuilder failureMsg = new StringBuilder();
			ImportExcel ei = new ImportExcel(file, 1, 0);
			List<CrowdLeadershipTeach> list = ei.getDataList(CrowdLeadershipTeach.class);
			for (CrowdLeadershipTeach crowdLeadershipTeach : list){
				try{
					crowdLeadershipTeachService.save(crowdLeadershipTeach);
					successNum++;
				}catch(ConstraintViolationException ex){
					failureNum++;
				}catch (Exception ex) {
					failureNum++;
				}
			}
			if (failureNum>0){
				failureMsg.insert(0, "，失败 "+failureNum+" 条领导班子(教学群众)记录。");
			}
			return AjaxJson.success( "已成功导入 "+successNum+" 条领导班子(教学群众)记录"+failureMsg);
		} catch (Exception e) {
			return AjaxJson.error("导入领导班子(教学群众)失败！失败信息："+e.getMessage());
		}
    }

	/**
	 * 下载导入领导班子(教学群众)数据模板
	 */
	@RequiresPermissions("ticket:crowdleadershipteach:crowdLeadershipTeach:import")
    @GetMapping("import/template")
     public AjaxJson importFileTemplate(HttpServletResponse response) {
		try {
            String fileName = "领导班子(教学群众)数据导入模板.xlsx";
    		List<CrowdLeadershipTeach> list = Lists.newArrayList();
    		new ExportExcel("领导班子(教学群众)数据", CrowdLeadershipTeach.class, 1).setDataList(list).write(response, fileName).dispose();
    		return null;
		} catch (Exception e) {
			return AjaxJson.error( "导入模板下载失败！失败信息："+e.getMessage());
		}
    }


}